<template>
  <div style="position: relative">
    <a-form
      ref="formRef"
      layout="vertical"
      :model="formState"
      :rules="rules"
      :wrapper-col="wrapperCol"
      style="display: flex; flex-wrap: wrap"
    >
      <a-form-item style="width: 50%" ref="realName" label="管理员名称" name="realName">
        <a-input v-model:value="formState.realName" placeholder="请输入管理员名称" />
      </a-form-item>
      
      <a-form-item
        style="width: 50%; padding-left: 15px"
        ref="mobile"
        label="管理员手机号"
        name="mobile"
      >
        <a-input
          v-model:value="formState.mobile"
          placeholder="请输入管理员手机号"
        />
      </a-form-item>
    
    </a-form>

   
  </div>
</template>
<script>
import {
  defineComponent,
  onMounted,
  reactive,
  ref,
  toRaw,
  toRef,
  toRefs,
  watch,
} from "vue";
import * as sysUserAdminApi from "@/api/userAdmin.js";
import { message } from "ant-design-vue";
let validatePhone = async (rule, value) => {
  // var reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
  var reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
  if (value == "") {
    return Promise.reject("请输入手机号");
  } else if (!reg.test(value)) {
    return Promise.reject("请输入正确手机号");
  } else {
    return Promise.resolve();
  }
};

const rules = {
  realName: [
    {
      required: true,
       message: "请输入管理员名称",
      trigger: "blur",
    },
  ],
  
  mobile: [
    {
      required: true,
      validator: validatePhone,
      message: "请输入正确手机号",
      trigger: "blur",
    },
  ],
};

export default defineComponent({
  props: {
    handleType: {
      type: String,
      default: "add",
    },
    handleRow: {
      type: Object,
      default: {},
    },
  },
  setup(props, { emit }) {
    const handleType = toRef(props, "handleType");
    const formRef = ref();
    const formState = reactive({
      realName: "", //管理员名称
      mobile: "", //管理员手机号
      id:''
    });
    
    // 保存数据
    const handleSave = () => {
      if (props.handleType == "look") {
        resetForm();
        emit("finish");
        return false;
      }
      formRef.value.validate().then(() => {
        let params = {
          ...toRaw(formState),
          sysUserId:props.handleRow.sysUserId
        };
         sysUserAdminApi
            .sysUserAdminSave(params).then((res) => {
          if (res && res.success) {
            message.success("操作成功");
            resetForm();
            emit("finish");
          } else {
          }
        });
      });
    };
    const resetForm = () => {
      formRef.value.resetFields();
    };
    onMounted(async () => {
      if (props.handleType == "add") {
        formState.id = ''
        return false;
      }
      formState.realName = props.handleRow.realName
      formState.mobile = props.handleRow.mobile
      formState.id = props.handleRow.id
     
    });

   
    return {
      formRef,
      handleType,
      wrapperCol: {
        span: 0,
      },
      formState,
      rules,
      handleSave,
      resetForm,
    };
  },
});
</script>
<style lang="scss">

</style>